// https://leetcode.cn/problems/binary-tree-level-order-traversal/
// Created by ade on 2022/7/14.
// 给你二叉树的根节点 root ，返回其节点值的 层序遍历 。 （即逐层地，从左到右访问所有节点）。

#include <iostream>
#include <vector>


using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int height = 0;
    vector <vector<int>> res;

    vector <vector<int>> levelOrder(TreeNode *root) {
        dfs(root, 0);
        return res;
    }

    void dfs(TreeNode *root, int index) {
        if (!root) return;
        if (index >= res.size()) res.push_back({});

        res[index].push_back(root->val);
        dfs(root->left, index + 1);
        dfs(root->right, index + 1);
    }


    TreeNode *init() {
        TreeNode *n1 = new TreeNode(1);
        TreeNode *n2 = new TreeNode(2);
        TreeNode *n3 = new TreeNode(2);
        TreeNode *n4 = new TreeNode(3);
        TreeNode *n5 = new TreeNode(3);
        n1->left = n2;
        n1->right = n3;
        n2->left = n4;
        n3->left = n5;

        return n1;
    }
};


int main() {
    Solution so;
    TreeNode *h1 = so.init();
    auto res = so.levelOrder(h1);
    for (auto items : res) {
        for (auto i : items) {
            cout << i << ",";
        }
        cout << endl;
    }
    return 0;
}